<?php
require_once 'conexao.php';
?>
<html>
    <link rel="stylesheet" type="text/css" href="style.css"/>
    <body id="relatorio">
        <table align="left" border="1" cellpadding="1" cellspacing="1" style="width: 1210px; height: 93px;">

            <tr>
                <td style="width: 200px; height: 70px;">
                    <img alt="" src="http://www.saoexpedito.com.br/imagens/logo.jpg" style="width: 201px; height: 70px;" /></td>
                <td style="text-align: center;"><h1>RELATÓRIO DE TEMPO DE OPERAÇÃO - PRESTADOR</h1></td>
                <td style="width: 150px;">
                    Rev.: 05<br />
                    Data: 09/11/2009<br />
                    P&aacute;gina 1 de 1</td>
            </tr>

        </table>
        <p>
            <?php
            $datas = explode('/', $_POST['dt_entrada_ini']);
            $datade = $datas['2'] . "-" . $datas['1'] . "-" . $datas['0'];
            $datas = explode('/', $_POST['dt_entrada_fim']);
            $dataate = $datas['2'] . "-" . $datas['1'] . "-" . $datas['0'];
            $sql = "SELECT *, TIMEDIFF(CONCAT(dt_saida,' ', hr_saida), CONCAT(dt_entrada,' ', hr_entrada)) AS diferenca FROM prestador WHERE port_saida <> ' ' AND dt_entrada >='" . $datade . "' AND dt_entrada <= '" . $dataate . "'";

            if ($_POST['placa'] <> '*') {
                $sql = $sql . " and placa = '" . $_POST['placa'] . "'";
            }
            if ($_POST['motorista_entrada'] <> '*' && $_POST['motorista_saida'] <> '*') {
                $sql = $sql . " and (mot_entrada = '" . $_POST['motorista_entrada'] . "' or mot_saida = '" . $_POST['motorista_saida'] . "') ";
            } else {

                if ($_POST['motorista_entrada'] <> '*') {
                    $sql = $sql . " and mot_entrada = '" . $_POST['motorista_entrada'] . "'";
                }
                if ($_POST['motorista_saida'] <> '*') {
                    $sql = $sql . " and mot_saida = '" . $_POST['motorista_saida'] . "'";
                }
            }
            $sql = $sql . "ORDER BY diferenca DESC";
            $result = mysql_query($sql);
            ?>
        </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <table border='0' width="1210" name="tabela_rel">

            <tr style="background-color: lightgray; font-weight: bold">
                <td align="center">PLACA</td>
                <td align="center">TEMPO</td>
                <td align="center">DATA ENTRADA</td>
                <td align="center">HORA ENTRADA</td>
                <td align="center">PORT. ENTRADA</td>
                <td align="center">DATA SAÍDA</td>
                <td align="center">HORA SAÍDA</td>
                <td align="center">PORT. SAÍDA</td>
                <td align="center">MOTORISTA</td>
                <td align="center">OPERAÇÃO</td>
            </tr>

            <?php
            echo mysql_error();
            $c = 1;
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $datas = explode('-', $row['dt_entrada']);
                $datade = $datas['2'] . "/" . $datas['1'] . "/" . $datas['0'];
                $datas = explode('-', $row['dt_saida']);
                $dataate = $datas['2'] . "/" . $datas['1'] . "/" . $datas['0'];
                if ($row['dt_saida'] == '0000-00-00') {
                    $row['mot_saida'] = '-';
                    $row['hr_saida'] = '-';
                    $row['port_saida'] = '-';
                    $dataate = '-';
                }

                $separa = explode(':',$row['diferenca']);
                echo"
                <tr>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . ($row['placa']) .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $separa[0].'Hr(s) '.$separa[1] .'Min'.
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $datade .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['hr_entrada'] .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['port_ent'] .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $dataate .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['hr_saida'] .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['port_saida'] .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['motorista'] .
                "</td>
            <td align='center' valign='middle' style='border-bottom: 1px solid black'>" . $row['operacao'] .
                "</td>
                    </tr>";

                $c++;
            }
            ?>
        </table>
    </body>
</html>